<?php
include("include.php");
//include ("../library/php/class_sanpham.php");
$sanpham = new class_sanpham();

$catid = 0;
$subcatid = 0;
$title_luu = $arr_title_timkiem[$langid];

//Xu ly bien moi truong
if ($REQUEST_METHOD == "POST"){
	session_unregister('session_tukhoa');	$session_tukhoa = $tukhoa;	session_register('session_tukhoa');	
}
$tukhoa = trim($session_tukhoa);
$q = $tukhoa;           // grab the search string the user entered
$q = preg_replace("/ +/", ' ', $q);
//$q = str_replace(array("\\",";"), "", $q);  // remove ALL backslashes & remove ALL ";" -> for sql security: no (simple) injection of commands
$q = trim($q);


$strSql= "SELECT * FROM sanpham ";
$strSql.=" WHERE (name like '%$q%' or maso like '%$q%') and ngonngu='$langid' ";

//echo "strSql: $strSql<br>";

$str_paging = $sanpham->PageList($strSql);
$total_rows = $sanpham->TotalRows($strSql);
//echo "total_rows: $total_rows<br>";

if ($langid == 1){
	$str_ketqua = "Found <b>$total_rows</b> product";
}else{
	$str_ketqua = "T&igrave;m th&#7845;y <b>$total_rows</b> s&#7843;n ph&#7849;m";
}

//Layout Loading
include("html_layout.php");
$str_tmp = loadHtml("../htmls_php/tmp_mainpro.htm",$msg);
$str_htm = ereg_replace("@content@",$str_tmp,$str_htm);
//==================== Hien thi san pham ==
if ($tukhoa != "")
{
	//Tinh so column
	$column = 2;
	//echo "column: $column<br>";
	
	//=========== Begin san pham ========================

	$strSql.=" ORDER BY maso  ";
	$strSql.=" LIMIT ".$sanpham->limit01.", ".$sanpham->limit02;
	//echo ("sanpham: $strSql<br>");
	$myData= new db_query($dbconn,$strSql);
	$sodong=$myData->numrows();
	//echo ("so dong: $sodong<br>"); //du thua
	$i=1;
	$str_sanpham_begin= get_values("select template_begin from templates where template_id ='105'");
	//echo  "<pre>".htmlspecialchars($str_sanpham_begin)."</pre>";
	
	$str_sanpham = $str_sanpham_begin;
	$col=0;
	
	/////////////////co san pham//////////////////////
	$upload_path = AddSlashes("../uploads/sanpham/small/");
	$upload_path_blank = AddSlashes("../uploads/sanpham/small/photo_blank.gif");
	
	$popup_path = AddSlashes("../uploads/sanpham/popup/");
	$popup_path_blank = AddSlashes("../uploads/sanpham/popup/photo_blank.gif");
	
	//Thong so san pham
	$tmp_thongso = $sanpham->ThongsoSanpham($langid);

	$myData= new db_query($dbconn,$strSql);
	$tmp_giatri = "";
	while ($myData->getrow())
	{
		//hien thi header
		$header_status=1;	
		$fld_tmpid= $myData->row["tmpid"];
		$fld_id = $myData->row["proid"];		
		$img1 = $myData->row["imagepath"];
	
				
		$ten = $myData->row["name"];
		$maso = $myData->row["maso"];
		$quycach = $myData->row["fd01"];
	$gia = (int)$myData->row["fd02"];
	
	$chitiet = "";
	if ($gia > 0){
		$chitiet .= number_format($gia,0,",",".")." VND";
	}	
		if($maso !=""){
			$chitiet .= "<br><b>$maso</b>";
		}
		if($ten != ""){
			$chitiet .= "<br>$ten";
		}
		if($quycach != ""){
			$chitiet .= "<br>($quycach)";
		}
						
		if($col==0){
			$str_sanpham.="<tr>"; 
		}
		//load template
		if ($tmp_giatri == ""){
			$tmpSql="select template_value from templates where template_id = '$fld_tmpid'";
			$tmpData= new db_query($dbconn,$tmpSql);
			if($tmpData->getrow())
			{
				$tmp_giatri = $tmpData->row[0];
				$str_sanpham.="<td align=center valign=top>".$tmp_giatri."</td>";
			}
		}else{
			$str_sanpham.="<td align=center valign=top>".$tmp_giatri."</td>";
		}

		if ($img1 == ""){
			$img1 = $myData->row["link_hinhnho"];
		}
			  
		if($img1 != ""){
			$img_fullpath = $upload_path.$img1;
			if (file_exists($img_fullpath)){
				$popsize=getimagesize($img_fullpath) ;
				$img_width = htmlspecialchars($popsize[0]);
				$img_height = htmlspecialchars($popsize[1]);
				//echo "img_width: $img_width - img_height: $img_height<br>";
				if ($img_width > SP_IMAGES_SMALL_WIDTH){
					$img_tile = ceil((SP_IMAGES_SMALL_WIDTH*100)/$img_width);
					$img_width = SP_IMAGES_SMALL_WIDTH;
					$img_height = ceil(($img_tile*$img_height)/100);
					//echo "img_tile: $img_tile - img_width: $img_width - img_height: $img_height<br>";
				}																
				$value_replacement = "<img src='".$img_fullpath."' width='$img_width' height='$img_height' border=0>";
				$value_replacement = "<a href=\"javascript:OpenWindow('popup.php?popupid=@popupid@&langid=@langid@','','width=@popup_width@,height=@popup_height@,toolbar=no,location=no,status=no,menubar=no,resizable=yes,scrollbars=yes,alwaysRaised,dependent,titlebar=no')\">$value_replacement</a>";
			}else{
				$value_replacement = "&nbsp";
			}
		}else{
			$value_replacement = "&nbsp";
		}
	
		$return_page= "search.php?current_pages=".$current_pages."__catid=".$catid."__subcatid=".$subcatid."__langid=".$langid;
		//echo "return_page: $return_page<br>";
		$link_addcart = "addcart.php?proid=@popupid@&langid=@langid@&return_page=$return_page";
		//Cart
		$str_sanpham=ereg_replace("@link_addcart@","$link_addcart",$str_sanpham);	
		
		//Hien thi hinh anh
		$str_sanpham=ereg_replace("@image@",$value_replacement,$str_sanpham);
		$str_sanpham=ereg_replace("@chitiet@", "$chitiet", $str_sanpham);
	
					
		//Popup
		$str_sanpham=ereg_replace("@popupid@",$fld_id,$str_sanpham);
		$str_sanpham=ereg_replace("@popup_width@", POPUP_WIDTH,$str_sanpham);
		$str_sanpham=ereg_replace("@popup_height@", POPUP_HEIGHT,$str_sanpham);
		$str_sanpham=ereg_replace("@langid@",$langid,$str_sanpham);			
		$col++;
		//print "<br>col=".$col;
		if($col==$column)
		{
			$str_sanpham.="</tr>";
			$col=0;
		}
		//print "<br>i=".$i;
		$i++;
		
	
	}
	$str_sanpham.="</table>";
	$dbconn->stmtclose();
	//=============== End San Pham =============================
		
	//thay the noi dung
	$str_htm = ereg_replace("@header@",$str_header,$str_htm);	
	$str_htm = ereg_replace("@cat_parent@",$parentid,$str_htm);
	$str_htm = ereg_replace("@list_products@",$str_sanpham,$str_htm);
	$str_htm = ereg_replace("@PAGE@", $str_paging, $str_htm);
	
	$str_htm = ereg_replace("@current_pages@", "$current_pages", $str_htm);
	$str_htm = ereg_replace("@catid@", "$catid", $str_htm);
	$str_htm = ereg_replace("@subcatid@", "$subcatid", $str_htm);
	$str_htm = ereg_replace("@langid@", "$langid", $str_htm);
	$str_htm = ereg_replace("@xemtheonam@", "$str_ketqua", $str_htm);	
} //End if tukhoa 

//=========================================


//Header Loading - CSS, Meta, Javascript ...
include("html_header.php");

//Body Loading - Date-Time, ...
include("html_body.php");	

//Menu include
include("html_menu.php");

//Modules include
include("modules.php");	

print $str_htm;
?>